cmake_minimum_required(VERSION 3.25)
project(DesignPattern)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fmodules-ts")

# 定义一个全局的list变量
set(GLOBAL_LIST "" CACHE INTERNAL "Global list")
# 添加元素到全局list
function(append_to_global_list value)
    set(GLOBAL_LIST "${GLOBAL_LIST}" ${value} CACHE INTERNAL "Global list" FORCE)
endfunction()

#findSrc 函数名
#dirPath 参数
function(findSrc dirPath)
    # 设定要遍历的目录，保存到 VAC_INCLUDE_DIR 变量
    set(VAC_INCLUDE_DIR ${dirPath})

    # 获取指定目录下的所有内容，并将获取到的内容保存到变量 ALL_ITEMS
    file(GLOB ALL_ITEMS ${VAC_INCLUDE_DIR}/*)
    foreach (ITEM ${ALL_ITEMS})
        # 找出.cpp或者.h文件
        STRING(FIND ${ITEM} ".cpp" pos1)
        STRING(FIND ${ITEM} ".h" pos2)
        if (pos1 GREATER -1 OR pos2 GREATER -1)
            append_to_global_list(${ITEM})
        endif ()
        if (IS_DIRECTORY ${ITEM})
            findSrc(${ITEM})
        endif ()
    endforeach ()
endfunction()

findSrc(${CMAKE_SOURCE_DIR}/Src)

# 在其他地方通过以下命令获取这个list
set(GLOBAL_LIST "${GLOBAL_LIST}" CACHE INTERNAL "Global list" FORCE)

add_executable (DesignPattern ${GLOBAL_LIST})
